package com.nutshellroid.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public  class NSRDateTime {
	static SimpleDateFormat sDateFormatdate=new SimpleDateFormat("yyyy-MM-dd");
	static Date nowtime;
	static String thisDate;
	
	public NSRDateTime(String date) {
		thisDate = new String(date);
	}
	
	public static String getNowDate()
    {
    	nowtime = new Date(); 
    	String now= sDateFormatdate.format(nowtime);
    	return now;
    }
    
    public static String getDateLeft(String date, int daysBefore)
    {
    	Calendar c = Calendar.getInstance();
    	try {
			c.setTime(sDateFormatdate.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	c.add(Calendar.DATE, -daysBefore);
    	return sDateFormatdate.format(c.getTime());
    }
    
    public static String getDateRight(String date, int daysAfter)
    {    	
    	Calendar c = Calendar.getInstance();
    	
    	try {
			c.setTime(sDateFormatdate.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		c.add(Calendar.DATE, daysAfter);
    	
    	return sDateFormatdate.format(c.getTime());
    }
    
    public static boolean beforeToday(String date) {
    	Calendar c = Calendar.getInstance();
    	Calendar cNow = Calendar.getInstance();
    	
    	try {
			c.setTime(sDateFormatdate.parse(date));
			cNow.setTime(sDateFormatdate.parse(getNowDate()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		if (c.before(cNow)) {
			return true;
		}
		
		return false;
	}
    
    public static boolean afterToday(String date) {
    	Calendar c = Calendar.getInstance();
    	Calendar cNow = Calendar.getInstance();
    	
    	try {
			c.setTime(sDateFormatdate.parse(date));
			cNow.setTime(sDateFormatdate.parse(getNowDate()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		if (c.after(cNow)) {
			return true;
		}
		
		return false;
	}
    
    public static long dateDiffFromToday(String date) {

    	long nd = 1000*24*60*60;
    	long diff;
    	try {
    		long dateTime = sDateFormatdate.parse(date).getTime();
    		long nowTime = sDateFormatdate.parse(NSRDateTime.getNowDate()).getTime();
    		diff = dateTime - nowTime;
    		
    		return diff/nd;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return 0;
	}
    
    public static long dateDiff(String ndate, String odate) {

    	long nd = 1000*24*60*60;
    	long diff;
    	try {
    		long nTime = sDateFormatdate.parse(ndate).getTime();
    		long oTime = sDateFormatdate.parse(odate).getTime();
    		diff = nTime - oTime;
    		
    		return diff/nd;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return 0;
	}
    
    public boolean before(String date) {
    	Calendar cSelf = Calendar.getInstance();
    	Calendar cDate = Calendar.getInstance();
    	
    	try {
			cSelf.setTime(sDateFormatdate.parse(thisDate));
			cDate.setTime(sDateFormatdate.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		if (cSelf.before(cDate)) {
			return true;
		}
		
		return false;
	}
    
    public boolean after(String date) {
    	Calendar cSelf = Calendar.getInstance();
    	Calendar cDate = Calendar.getInstance();
    	
    	try {
			cSelf.setTime(sDateFormatdate.parse(thisDate));
			cDate.setTime(sDateFormatdate.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		if (cSelf.after(cDate)) {
			return true;
		}
		
		return false;
	}
    
    public void	addDays(int days) {
    	thisDate = getDateRight(thisDate, days);
	}
    
    public void subDays(int days) {
    	thisDate = getDateLeft(thisDate, days);
    }
    
    public long diff(String date) {
    	return dateDiff(thisDate, date);
	}
}
